package com.example.threebuttonapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {
    private EditText vibrationDurationInput;
    private EditText vibrationCooldownInput;
    private EditText recordingTimeInput;
    private EditText volumeRatioInput;
    private Switch soundSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // 添加返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        vibrationDurationInput = findViewById(R.id.vibrationDuration);
        vibrationCooldownInput = findViewById(R.id.vibrationCooldown);
        recordingTimeInput = findViewById(R.id.recordingTime);
        volumeRatioInput = findViewById(R.id.volumeRatio);
        soundSwitch = findViewById(R.id.soundSwitch);
        Button saveButton = findViewById(R.id.saveButton);

        // 获取从 MainActivity 传递过来的参数
        Intent intent = getIntent();
        if (intent != null) {
            vibrationDurationInput.setText(String.valueOf(intent.getIntExtra("vibrationDuration", 4000))); // 默认值
            vibrationCooldownInput.setText(String.valueOf(intent.getIntExtra("vibrationCooldown", 3000))); // 默认值
            recordingTimeInput.setText(String.valueOf(intent.getIntExtra("recordingTime", 10))); // 默认值
            volumeRatioInput.setText(String.valueOf(intent.getDoubleExtra("volumeRatio", 1.5))); // 默认值
        }

        // 从 SharedPreferences 中读取铃声开关状态
        SharedPreferences sharedPreferences = getSharedPreferences("app_settings", MODE_PRIVATE);
        boolean isSoundEnabled = sharedPreferences.getBoolean("sound_enabled", true); // 默认启用铃声
        soundSwitch.setChecked(isSoundEnabled);

        saveButton.setOnClickListener(v -> saveSettings());
    }

    private void saveSettings() {
        try {
            // 获取用户输入的参数
            int vibrationDuration = Integer.parseInt(vibrationDurationInput.getText().toString());
            int vibrationCooldown = Integer.parseInt(vibrationCooldownInput.getText().toString());
            int recordingTime = Integer.parseInt(recordingTimeInput.getText().toString());
            double volumeRatio = Double.parseDouble(volumeRatioInput.getText().toString());
            boolean isSoundEnabled = soundSwitch.isChecked(); // 获取铃声开关状态

            // 将参数传回 MainActivity
            Intent resultIntent = new Intent();
            resultIntent.putExtra("vibrationDuration", vibrationDuration);
            resultIntent.putExtra("vibrationCooldown", vibrationCooldown);
            resultIntent.putExtra("recordingTime", recordingTime);
            resultIntent.putExtra("volumeRatio", volumeRatio);
            setResult(RESULT_OK, resultIntent);

            // 保存铃声开关状态到 SharedPreferences
            SharedPreferences sharedPreferences = getSharedPreferences("app_settings", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("sound_enabled", isSoundEnabled);
            editor.apply();

            finish();
        } catch (NumberFormatException e) {
            // 处理输入格式错误
            Toast.makeText(this, "请输入有效的数字", Toast.LENGTH_SHORT).show();
        }
    }

    // 处理返回按钮点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish(); // 关闭当前活动
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 